/**
 * Copyright 2012 Fonfon.Fenceer
 * All rights reserved.
 * 
 * @project 
 * @author Fonfon.Fenceer
 * @version 1.0
 * @data 2012-3-9
 */
package com.guidecaptain.java.action;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.guidecaptain.common.validatecode.service.ValidateCodeService;
import com.guidecaptain.core.base.action.BaseAction;
import com.guidecaptain.core.common.CookieManager;
import com.guidecaptain.java.model.pojo.User;
import com.guidecaptain.java.model.vo.UserVO;
import com.guidecaptain.java.service.UserService;

@Controller
@Scope("prototype")
public class UserAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Logger LOG = Logger.getLogger(UserAction.class);

	@Autowired
	private UserService userService;
	@Autowired
	private ValidateCodeService validateCodeService;

	private UserVO userVO = new UserVO();
	
	/**
	 * 用户登录
	 */
	public String login() {
		CookieManager cookieManager = new CookieManager(getRequest(), getResponse());
		JSONObject jsonObject = new JSONObject();

		// 验证码是否正确
		boolean checkCode = validateCodeService.isCheckValidateCode(cookieManager, userVO.getCode());
		if (!checkCode) {
			jsonObject.put("state", "checkCode");
			setJsonObject(jsonObject);

			return JSON;
		}

		User user = userService.login(userVO.getUsername(), userVO.getPsw());

		// 用户不存在或密码错误
		if (user == null) {
			jsonObject.put("state", "checkPsw");
			setJsonObject(jsonObject);

			return JSON;
		}

		// 如果用户权限大于请求权限，则通过
		if (user.getRole() >= userVO.getRole()) {
			switch (userVO.getRole()) {
			case 9:
				jsonObject.put("state", "system");
				break;
			case 7:
				jsonObject.put("state", "manager");
				break;
			case 5:
				jsonObject.put("state", "officer");
				break;
			case 3:
				jsonObject.put("state", "guide");
				break;
			}

			try {
				PropertyUtils.copyProperties(userVO, user);
				cookieManager.addCookie("username", userVO.getUsername());
				cookieManager.addCookie("role", String.valueOf(userVO.getRole()));
				cookieManager.addCookie("userId", String.valueOf(userVO.getUserId()));

			} catch (Exception e) {
				LOG.error(e);

				jsonObject.put("state", "fail");
			}

		} else {

			jsonObject.put("state", "checkRole");
		}

		setJsonObject(jsonObject);
		return JSON;
	}
	
	/**
	 * 检查用户名是否已经存在
	 */
	public String checkUsername() {
		User user = userService.queryUser(userVO.getUsername());
		JSONObject jsonObject = new JSONObject();
		if (user == null) {
			jsonObject.put("state", true);// 表示用户名不存在
		} else {
			jsonObject.put("state", false);// 表示用户名已存在
		}
		
		setJsonObject(jsonObject);
		return JSON;
	}
	
	/**
	 * 增加用户
	 */
	public String insertUser() {
		CookieManager cookieManager = new CookieManager(getRequest(), getResponse());
		JSONObject jsonObject = new JSONObject();
		
		// 检查权限
		int role=Integer.parseInt(cookieManager.getCookieValue("role"));
		if (role<5) {
			jsonObject.put("state", "checkRole");
			setJsonObject(jsonObject);

			return JSON;
		}

		// 验证码是否正确
		boolean checkCode = validateCodeService.isCheckValidateCode(cookieManager, userVO.getCode());
		if (!checkCode) {
			jsonObject.put("state", "checkCode");
			setJsonObject(jsonObject);

			return JSON;
		}

		// 检查用户名是否已经存在
		User user = userService.queryUser(userVO.getUsername());
		if (user != null) {
			jsonObject.put("state", "checkUserName");
			setJsonObject(jsonObject);

			return JSON;
		}
		
		//没有密码设置默认密码
		if(userVO.getPsw()==null){
			userVO.setPassword("123456");
		}
		
		if (userService.insertUser(userVO) > 0) {
			jsonObject.put("state", "insert");

		}else {
			jsonObject.put("state", "insertFail");

		}
		
		setJsonObject(jsonObject);
		return JSON;
	}
	
	/**
	 * 用户列表
	 */
	public String userList() {
		CookieManager cookieManager = new CookieManager(getRequest(), getResponse());
		System.out.println("iicc"+cookieManager.getCookieValue("username"));
		userVO.setUserList(userService.queryUserList());
		return "userList";
	}
	
	/**
	 * 安全退出
	 */
	public String logout() {
		CookieManager cookieManager = new CookieManager(getRequest(), getResponse());
		cookieManager.clear();
		return "logout";
	}
	
	/**
	 * 修改密码
	 */
	public String changePsw(){
		JSONObject jsonObject=new JSONObject();
		if(userVO.getPsw()==null){
			userVO.setPassword("123456");
		}
		if(this.userService.updateUser(userVO)>0){
			jsonObject.put("state", true);
		}else {
			jsonObject.put("state", false);
		}
		
		setJsonObject(jsonObject);
		return JSON;
	}
	
	/**
	 * 修改角色
	 */
	public String changeRole(){
		User user=new User();
		user.setUserId(userVO.getUserId());
		user.setRole(userVO.getRole());
		
		JSONObject jsonObject=new JSONObject();
		if(this.userService.updateUser(user)>0){
			jsonObject.put("state", true);
		}else {
			jsonObject.put("state", false);
		}
		setJsonObject(jsonObject);
		return JSON;
	}
	

	@Override
	public Object getModel() {
		return userVO;
	}

}
